home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / eiffel / smalleif.97 / se.t / SmallEiffel / lib_std / comparable.e < prev    next >
Encoding:
Text File  |  1996-05-02  |  2.7 KB  |  115 lines

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. deferred class COMPARABLE
  5.    --
  6.    -- All class handling COMPARABLE object with a total order relation
  7.    -- should inherit from this class.
  8.    --
  9.    
  10. inherit ANY redefine is_equal end;
  11.    
  12. feature {ANY}
  13.    
  14.    infix "<" (other: like Current): BOOLEAN is
  15.      -- Is 'Current' strictly less than 'other'?
  16.       require
  17.      other_exists: other /= Void
  18.       deferred
  19.       ensure
  20.      asymmetric: Result implies not (other < Current);
  21.       end;
  22.    
  23.    infix "<=" (other: like Current): BOOLEAN is
  24.      -- Is 'Current' less or equal 'other'?
  25.       require
  26.      other_exists: other /= Void
  27.       do
  28.      Result := not (Current > other)
  29.       ensure
  30.      definition: Result = (Current < other) or is_equal(other);
  31.       end;
  32.  
  33.    infix ">" (other: like Current): BOOLEAN is
  34.      -- Is 'Current' strictly greater than 'other'?
  35.       require
  36.      other_exists: other /= Void
  37.       do
  38.      Result := (other < Current)
  39.       ensure
  40.      definition: Result = (other < Current)
  41.       end;
  42.  
  43.    infix ">=" (other: like Current): BOOLEAN is
  44.      -- Is 'Current' greater or equal than 'other'?
  45.       require
  46.      other_exists: other /= Void
  47.       do
  48.      Result := not (Current < other)
  49.       ensure
  50.      definition: Result = (other <= Current)
  51.       end;
  52.    
  53.    is_equal(other: like Current): BOOLEAN is
  54.       do
  55.      if Current < other then
  56.      elseif other < Current then
  57.      else
  58.         Result := true;
  59.      end;
  60.       ensure then
  61.      trichotomy: Result = (not (Current < other) 
  62.                    and not (other < Current));
  63.       end;
  64.       
  65.    compare(other: like Current): INTEGER is
  66.      -- Compare 'Current' with 'other'.
  67.      -- '<' <==> Result < 0
  68.      -- '>' <==> Result > 0
  69.      -- Otherwise Result = 0.
  70.       require
  71.      other /= Void
  72.       do
  73.      if Current < other then
  74.         Result := -1
  75.      elseif other < Current then
  76.         Result := 1
  77.      end
  78.       ensure
  79.      (Result < 0) = (Current < other);
  80.      (Result = 0) = not (Current < other or Current > other);
  81.      (Result > 0) = (Current > other)
  82.       end;
  83.    
  84.    min(other: like Current): like Current is 
  85.      -- Minimum of 'Current' and 'other'.
  86.       require
  87.      other /= Void
  88.       do
  89.      if Current < other then
  90.         Result := Current
  91.      else
  92.         Result := other
  93.      end;
  94.       ensure
  95.      Result <= Current and then Result <= other;
  96.      compare(Result) = 0 or else other.compare(Result) = 0
  97.       end;
  98.  
  99.    max(other: like Current): like Current is 
  100.      -- Maximum of 'Current' and 'other'.
  101.       require
  102.      other /= Void
  103.       do
  104.      if other < Current then
  105.         Result := Current;
  106.      else
  107.         Result := other;
  108.      end;
  109.       ensure
  110.      Result >= Current and then Result >= other;
  111.      compare(Result) = 0 or else other.compare(Result) = 0
  112.       end;
  113.  
  114. end -- COMPARABLE
  115.